package com.lib.image

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.lib.image.ex.RxUtils
import com.zhihu.matisse.engine.ImageEngine
import com.zhihu.matisse.engine.impl.GlideEngine
import io.reactivex.rxjava3.core.Observable
import java.util.*


/**
 * @Author: shalei
 * @Date: 2019-10-28 13:09
 * @Desc: 上下文，谁使用就传谁，优先顺序，fragment->activity->view->全局的context
 **/
object ImageLoader {

    private var mImage: ImageEngine? = null

    @JvmStatic
    fun loadCircle(
        imageView: ImageView,
        img: String?,
        size: Int,
        errId: Int = 0,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadCircle(imageView, img, size, errId, activity, fragment, view, context)
    }

    fun loadBlur(
        imageView: ImageView,
        img: String?,
        blur: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadBlur(imageView, img, blur, activity, fragment, view, context)
    }

    fun loadBlur(
        imageView: ImageView,
        resId: Int,
        blur: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadBlur(imageView, resId, blur, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadCircle(
        imageView: ImageView,
        resId: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadCircle(imageView, resId, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadImage(
        imageView: ImageView,
        resId: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadThumbnail(imageView, resId, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadImage(
        imageView: ImageView,
        path: String?,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadThumbnail(imageView, path, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadDefaultImage(
        imageView: ImageView,
        path: String?,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadDefault(imageView, path, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadCornersImage(
        imageView: ImageView,
        path: String?,
        roundsCorners: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadCorners(imageView, path, roundsCorners, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadCornersImage(
        imageView: ImageView,
        bmp: Bitmap,
        roundsCorners: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadCorners(imageView, bmp, roundsCorners, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadFitCenterImage(
        imageView: ImageView,
        url: String,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadFitCenter(imageView, url, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadFitCenterImage(
        imageView: ImageView,
        bitmap: Bitmap,
        width: Int,
        height: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadFitCenterImage(imageView, bitmap, width, height, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadFitCenterImage(
        imageView: ImageView,
        url: String,
        width: Int,
        height: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadFitCenterImage(imageView, url, width, height, activity, fragment, view, context)
    }

    @JvmStatic
    fun loadCenterCropImage(
        imageView: ImageView,
        url: String,
        width: Int,
        height: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadCenterCropImage(imageView, url, width, height, activity, fragment, view, context)
    }

    /**
     * 带有默认加载图
     */
    @JvmStatic
    fun loadCenterCropImageP(
        imageView: ImageView,
        url: String,
        width: Int,
        height: Int,
        activity: Activity? = null,
        fragment: Fragment? = null,
        view: View? = null,
        context: Context? = null
    ) {
        if((activity == null && fragment == null && view == null && context == null)) return
        get().loadCenterCropImageP(imageView, url, width, height, activity, fragment, view, context)
    }

    @JvmStatic
    fun get(): ImageEngine {
        if (mImage == null) {
            mImage = GlideEngine()
        }
        return mImage!!
    }

    @JvmStatic
    fun clearDiskCache(context: Context) {
        Observable.create<String> {
            get().clearDiskCache(context)
            it.onComplete()
        }.compose(RxUtils.applySchedulers())
            .ignoreElements()
            .subscribe { }
    }

    @JvmStatic
    fun clearMemory(context: Context) {
        get().clearMemory(context)
    }

    @JvmStatic
    fun trimMemory(context: Context, level: Int) {
        get().trimMemory(context, level)
    }


}